/**
 * @file   AcctOpnLoadDB.java
 * @date   2012-12-18上午11:49:48
 * @author user
 */
package com.gzceb.zhgjj.fileload.impl;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.gzceb.zhgjj.filecheck.FileCheckConstants;
import com.gzceb.zhgjj.fileload.AbstractFileLoad;
import com.gzceb.zhgjj.service.AcctOpnService;
import com.gzceb.zhgjj.service.LoanAprvService;
import com.gzceb.zhgjj.util.SpringContextUtil;
import com.gzceb.zhgjj.util.Utils;
import com.gzceb.zhgjj.web.entity.AcctOpen;

/**
 * <p>客户开户文件加载进数据库类。</p>
 *
 * @date   2012-12-18上午11:49:48 
 * @author liqing
 * @description 客户开户文件的响应文件读取后，加载信息到 数据库表 info_acctopen
 */
public class AcctOpnLoadDB extends AbstractFileLoad {

	private static Log logger = LogFactory.getLog(AcctOpnLoadDB.class);
	
	private AcctOpen acctOpen;
	private String absFileName; // 响应文件的绝对路径
	private List<String> fileList;  // 筛选的审批记录中需要存库的记录
	
	public AcctOpnLoadDB(String absFileName){
		this.absFileName = absFileName;
		fileList = new ArrayList<String>();
	}
	
	
	public AcctOpen getAcctOpen() {
		return acctOpen;
	}


	public void setAcctOpen(AcctOpen acctOpen) {
		this.acctOpen = acctOpen;
	}


	/* (non-Javadoc)
	 * @see com.gzceb.zhgjj.fileload.AbstractFileLoad#run()
	 */
	@Override
	public void run() {
		readFile(absFileName);
		saveToDB();

	}

	/* (non-Javadoc)
	 * @see com.gzceb.zhgjj.fileload.AbstractFileLoad#saveToDB()
	 */
	@Override
	public void saveToDB() {
		AcctOpnService aos = (AcctOpnService) SpringContextUtil.getBean("acctOpnService");
		boolean rst = false;
		if ( 0 == fileList.size()){
			logger.info("the rsp file has no record to DB: " + absFileName);
		}else{
			// 从List中开始读取每一行的审批记录，循环记入库表
			for(int i=0; i< fileList.size(); i++){
				acctOpen.setPaperkind(fileList.get(i).substring(0, 2));
				acctOpen.setPaperid(fileList.get(i).substring(2, 20));
				acctOpen.setCustname(fileList.get(i).substring(20, 60).trim());
				acctOpen.setSexsign(fileList.get(i).substring(60, 61));
				acctOpen.setBirtdate(fileList.get(i).substring(61, 69));
				acctOpen.setNatisign(fileList.get(i).substring(69, 73));
				acctOpen.setRegiseat(fileList.get(i).substring(73, 79));
				acctOpen.setMarrsign(fileList.get(i).substring(79, 80));
				acctOpen.setEducsign(fileList.get(i).substring(80, 82));
				acctOpen.setVocasign(fileList.get(i).substring(82, 85));
				acctOpen.setTitlesign(fileList.get(i).substring(85, 87));
				acctOpen.setPrinciple(fileList.get(i).substring(87, 88));
				acctOpen.setWorkcorp(fileList.get(i).substring(88, 128).trim());
				acctOpen.setInco(Double.parseDouble(fileList.get(i).substring(128, 140)));
				acctOpen.setEnsuno(fileList.get(i).substring(140, 160));
				acctOpen.setProvince(fileList.get(i).substring(160, 166).trim());
				acctOpen.setCityname(fileList.get(i).substring(166, 186).trim());
				acctOpen.setCustaddr(fileList.get(i).substring(186, 266).trim());
				acctOpen.setAddrpost(fileList.get(i).substring(266, 273));
				acctOpen.setMobile(fileList.get(i).substring(273, 293));
				acctOpen.setFamitel(fileList.get(i).substring(293, 313));
				acctOpen.setCorptel(fileList.get(i).substring(313, 333));
				acctOpen.setEmail(fileList.get(i).substring(333, 373));
				acctOpen.setHandbankname(fileList.get(i).substring(373, 453));
				acctOpen.setBisikind(fileList.get(i).substring(453, 457));
				acctOpen.setLoankind(fileList.get(i).substring(457, 460));
				acctOpen.setFundcentno(fileList.get(i).substring(460, 464));
				acctOpen.setMgtdept(fileList.get(i).substring(464, 467));
				acctOpen.setLoantype(fileList.get(i).substring(467, 468));
				acctOpen.setRelaloanid(fileList.get(i).substring(468, 492));
				acctOpen.setFirstorder(fileList.get(i).substring(492, 493));
				acctOpen.setPayperc(Double.parseDouble(fileList.get(i).substring(493, 503)));
				acctOpen.setAssukind(fileList.get(i).substring(503, 507));
				acctOpen.setCondate(fileList.get(i).substring(507, 515));
				acctOpen.setLoanamt(Double.parseDouble(fileList.get(i).substring(515, 527)));
				acctOpen.setTermfreq(fileList.get(i).substring(527, 528));
				acctOpen.setLoanterm(Integer.parseInt(fileList.get(i).substring(528, 536)));
				acctOpen.setRetutype(fileList.get(i).substring(536, 540));
				acctOpen.setLoandueday(fileList.get(i).substring(540, 541));
				acctOpen.setSubsflag(fileList.get(i).substring(541, 542));
				acctOpen.setIsenousubs(fileList.get(i).substring(542, 543));
				acctOpen.setFirstpayamt(Double.parseDouble(fileList.get(i).substring(543, 555)));
				acctOpen.setFirstpayperc(Double.parseDouble(fileList.get(i).substring(555, 565)));
				acctOpen.setAddrid(fileList.get(i).substring(565, 569));
				acctOpen.setCorpid(fileList.get(i).substring(569, 589));
				acctOpen.setCorpname(fileList.get(i).substring(589, 629).trim());
				acctOpen.setProid(fileList.get(i).substring(629, 649));
				acctOpen.setProname(fileList.get(i).substring(649, 689).trim());
				acctOpen.setPayeeacno(fileList.get(i).substring(689, 719));
				acctOpen.setPayeeacname(fileList.get(i).substring(719, 759).trim());
				acctOpen.setPayeebankname(fileList.get(i).substring(759, 839).trim());
				acctOpen.setOutpayeeacno(fileList.get(i).substring(839, 869).trim());
				acctOpen.setOutpayeeacname(fileList.get(i).substring(869, 949).trim());
				acctOpen.setLoanprotype(fileList.get(i).substring(949, 951));
				acctOpen.setHousetype(fileList.get(i).substring(951, 953));
				acctOpen.setHouseformsign(fileList.get(i).substring(953, 955));
				acctOpen.setHousearea(Double.parseDouble(fileList.get(i).substring(955, 965)));
				acctOpen.setHouseamt(Double.parseDouble(fileList.get(i).substring(965, 975)));
				acctOpen.setHouseagreno(fileList.get(i).substring(975, 1001));
				acctOpen.setHouseaddr(fileList.get(i).substring(1001, 1101).trim());
				acctOpen.setHouseavgprice(Double.parseDouble(fileList.get(i).substring(1101, 1111)));
				acctOpen.setHousebuildyear(fileList.get(i).substring(1111, 1115));
				acctOpen.setHouseuseyear(Utils.stringToNoZero(fileList.get(i).substring(1115, 1123)));
				acctOpen.setContcode(fileList.get(i).substring(1123, 1155));
				acctOpen.setPrereledate(fileList.get(i).substring(1155, 1163));
				acctOpen.setBankid(fileList.get(i).substring(1163, 1172));
				acctOpen.setAcbankname(fileList.get(i).substring(1172, 1252).trim());
				acctOpen.setAcbankid(fileList.get(i).substring(1252, 1260));
				acctOpen.setFundacctno(fileList.get(i).substring(1260, 1288));
				acctOpen.setFundsavecard(fileList.get(i).substring(1288, 1312));
				acctOpen.setCorpfundacno(fileList.get(i).substring(1312, 1336));
				acctOpen.setCustfundacno(fileList.get(i).substring(1336, 1360));
				acctOpen.setCorpfundturnprop(Double.parseDouble(fileList.get(i).substring(1360, 1370)));
				acctOpen.setCustfundturnprop(Double.parseDouble(fileList.get(i).substring(1370, 1380)));
				acctOpen.setFundmonthturnamt(Double.parseDouble(fileList.get(i).substring(1380, 1392)));
				acctOpen.setFundturnbal(Double.parseDouble(fileList.get(i).substring(1392, 1404)));
				acctOpen.setFundturndate(fileList.get(i).substring(1404, 1410));
				acctOpen.setSubsacno(fileList.get(i).substring(1410, 1440));
				acctOpen.setAcname(fileList.get(i).substring(1440, 1480).trim());
				acctOpen.setBankname(fileList.get(i).substring(1480, 1560).trim());
				acctOpen.setRelatype(fileList.get(i).substring(1560, 1561));
				acctOpen.setRelacustid(fileList.get(i).substring(1561, 1601));
				acctOpen.setLiabtype(fileList.get(i).substring(1601, 1602));
				acctOpen.setLiabperc(Double.parseDouble(fileList.get(i).substring(1602, 1612)));
				acctOpen.setInptime(Utils.getCurrDateStr19Str());
				
				rst = aos.saveToDB(acctOpen);
			}
		}

	}


	@Override
	public void readFile(String absFileName) {
		// 将响应文件的内容去除不需存库的记录，生成List
		BufferedReader br = null;
		logger.info("begin to read rsp file: " + absFileName);
		try{
			br = new BufferedReader(new FileReader(absFileName));
			String tmpStr = null;
			boolean first = false;
			while( (tmpStr = br.readLine()) != null){
				if( false == first ){
					// 第一行信息是总笔数，跳过
					first = true;
					continue;
				}
				// 响应文件中每一行的末尾最后一个字节如果为9，表示接受，需要存库，记录到 List中
				if( tmpStr.endsWith(FileCheckConstants.RT_ACCEPT.getValue()) ){
					fileList.add(tmpStr);
				}
			}
		}catch(Exception e){
			logger.error("error happened when read rsp file: " + absFileName);
		}finally{
			if (br != null) {
				try {
					br.close();
				} catch (Exception ec) {
					logger.error("Errors happened when closinig BufferedReader.\n" + ec);
				}
			}
		}
		
		logger.info("the rsp file has been read to List. The list size is : " + fileList.size());
		
	}

}
